local awful   = require("awful")
local naughty = require("naughty")
require("main.vars")


function read_dev_state()
   local f = io.open(statesPath.."touchpadNotify", "r")
   local state = f:read()
   state = tonumber(state)
   f:close()

   return state
end

function notify_dev(devstate)
   if read_dev_state() == 1 then
      naughty.notify({
         title        = "Touchpad state",
         text         = devstate == 1 and "on" or "off",
         timeout      = 1,
         position     = "bottom_right",
         border_width = 4,
      })
   end
end

function toggle_dev_notify(dev)
   local devNoti = "touchpadNotify"
   local devName = "Touchpad"

   local f = io.open(statesPath..devNoti, "r")
   local state = 0
   
   if not f then
      f = io.open(statesPath..devNoti, "w")
      f:write("1")
      state = 1
   else
      state = (tonumber(f:read()) + 1) % 2
      f:write(state)
   end
   naughty.notify({
         title        = devName.." state notify",
         text         = state == 1 and "on" or "off",
         timeout      = 1,
         position     = "bottom_right",
         border_width = 4,
   })
   f:close()
end

function update_touch()
   local f = io.open(statesPath .. "touchpadState", "r")
   if not f then
      f = io.open(statesPath .. "touchpadState", "w")
      f:write("0")
   else
      local before = (tonumber(f:read()) + 1) % 2
   
      awful.spawn("xinput set-prop 'Synaptics TM3336-001' 'Device Enabled' "..before)
      notify_dev("touch", before)
      f:seek("set", 0)
      f:write(before)
   end
   f:close()

end
